/*
	Copyright (C) 2013 Pontificia Universidad Catolica del Peru
	contact: http://avatar.inf.pucp.edu.pe/realidad-aumentada/
	address contact: Av. Universitaria 1801, San Miguel, Lima-Peru.
	
	This file is part of UbicAR PUCP.

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with This program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
	Authors: Alvaro Roger Agüero Sánchez, Rodolfo Javier Galvez Meza
	Pontificia Universidad Catolica del Peru
	Departamento de Ingeniería
*/

package org.grupoavatar.guiaubicua.howto;
 
import org.grupoavatar.guiaubicua.R;
import org.grupoavatar.guiaubicua.VInicial;
import org.grupoavatar.guiaubicua.auxiliares.Usuario;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
 
public class CustomPageChangeListener extends android.support.v4.app.FragmentActivity implements OnPageChangeListener, OnClickListener {
 
//	private Button buttonCerrar	=	null;
//	private Button buttonCerrar1	=	null;
//	private Button buttonCerrar2 	=	null;
//	private Button buttonCerrar3 	=	null;
//	private Button buttonCerrar4 	=	null;
//	private Button buttonCerrar5 	= 	null;
//	private Button buttonCerrar6 	= 	null;
//	private Button buttonCerrar7 	=	null;
//	private Button buttonCerrar8 	= 	null;
//	private Button buttonCerrar9 	= 	null;
	
	private Button buttonComplemento 	=	null;
	private static Button cuenta_button	 	=	null;
	private static TextView cuenta_asociada = null;
	private Button buttonGPS 	=	null;
	private Button buttonWiFi 	=	null;
	
	
	private Activity context 	= 	null;
	//private GoogleMap mMap;
	/**
	 * Constructor
	 */
	public CustomPageChangeListener(Activity context) {
		this.context = context;
	}
 
 
	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void onPageSelected(int position) {
 
		switch (position) {
			case 0 : {
//				buttonCerrar1 = (Button) context.findViewById(R.id.buttonCerrar1);
//				buttonCerrar1.setOnClickListener(this);
				break;
			}
			case 1 : {
//				buttonCerrar2 = (Button) context.findViewById(R.id.buttonCerrar2);
//				buttonCerrar2.setOnClickListener(this);
				
				buttonComplemento = (Button) context.findViewById(R.id.buttonComplemento);
				buttonComplemento.setOnClickListener(this);
				
				int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
				TextView InstaladoTxt = (TextView) context.findViewById(R.id.InstaladoTxt);
				if(status != ConnectionResult.SUCCESS) {					
					InstaladoTxt.setText(R.string.configuracion_text_mapa_negativo);					
					InstaladoTxt.setTextColor(Color.RED);
					buttonComplemento.setVisibility(View.VISIBLE);						
				} else {														
					InstaladoTxt.setText(R.string.configuracion_text_mapa_afirmativo);
					InstaladoTxt.setTextColor(Color.BLUE);
				}											
				break;				
			}
			case 2 : {
//				buttonCerrar3 = (Button) context.findViewById(R.id.buttonCerrar3);
//				buttonCerrar3.setOnClickListener(this);
				
				cuenta_button = (Button) context.findViewById(R.id.cuenta_button);
				cuenta_asociada = (TextView) context.findViewById(R.id.cuenta_asociada);
				cuenta_button.setOnClickListener(this);
								
				buttonGPS = (Button) context.findViewById(R.id.buttonGPS);
				buttonGPS.setOnClickListener(this);
				
				buttonWiFi = (Button) context.findViewById(R.id.buttonWiFi);
				buttonWiFi.setOnClickListener(this);
				
				if (!Usuario.esValido) {
					cuenta_button.setVisibility(View.VISIBLE);
				} else {					
					cuenta_asociada.setVisibility(View.VISIBLE);
				}
				
				break;
			}
			case 3 : {
//				buttonCerrar4 = (Button) context.findViewById(R.id.buttonCerrar4);
//				buttonCerrar4.setOnClickListener(this);
				break;		
			}
			
			case 4 : {
//				buttonCerrar5 = (Button) context.findViewById(R.id.buttonCerrar5);
//				buttonCerrar5.setOnClickListener(this);
				break;		
			}
			
			case 5 : {
//				buttonCerrar6 = (Button) context.findViewById(R.id.buttonCerrar6);
//				buttonCerrar6.setOnClickListener(this);
				break;		
			}
			
			case 6 : {
//				buttonCerrar7 = (Button) context.findViewById(R.id.buttonCerrar7);
//				buttonCerrar7.setOnClickListener(this);
				break;		
			}
			
			case 7 : {
//				buttonCerrar8 = (Button) context.findViewById(R.id.buttonCerrar8);
//				buttonCerrar8.setOnClickListener(this);
				break;		
			}
			
			case 8 : {
//				buttonCerrar9 = (Button) context.findViewById(R.id.buttonCerrar9);
//				buttonCerrar9.setOnClickListener(this);
				break;		
			}
		}	
	}
 
	@Override
	public void onClick(View v) {
 
//		if (buttonCerrar != null && v.getId() == buttonCerrar.getId()) {
//			context.finish();
//		}
 
		// Click button yellow
		if (buttonComplemento != null && v.getId() == buttonComplemento.getId()) {
			String appName = "com.google.android.gms";
			try {
			    context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+appName)));
			} catch (android.content.ActivityNotFoundException anfe) {
				context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id="+appName)));
			}
		}	
		
//		if (buttonCerrar2 != null && v.getId() == buttonCerrar2.getId()) {
//			context.finish();
//		}
 
		// Click button red
//		if (buttonCerrar3 != null && v.getId() == buttonCerrar3.getId()) {
//			context.finish();
//		}	
		
		if (cuenta_button != null && v.getId() == cuenta_button.getId()) {
			VInicial.getUsername(context);
		}
		
		if (buttonGPS != null && v.getId() == buttonGPS.getId()) {			
			context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
		}
		
		if (buttonWiFi != null && v.getId() == buttonWiFi.getId()) {			
			context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
		}
		
//		if (buttonCerrar4 != null && v.getId() == buttonCerrar4.getId()) {			
//			context.finish();
//		}
		
//		if (buttonCerrar5 != null && v.getId() == buttonCerrar5.getId()) {			
//			context.finish();
//		}
		
//		if (buttonCerrar6 != null && v.getId() == buttonCerrar6.getId()) {			
//			context.finish();
//		}
		
//		if (buttonCerrar7 != null && v.getId() == buttonCerrar7.getId()) {			
//			context.finish();
//		}
		
//		if (buttonCerrar8 != null && v.getId() == buttonCerrar8.getId()) {			
//			context.finish();
//		}
		
//		if (buttonCerrar9 != null && v.getId() == buttonCerrar9.getId()) {			
//			context.finish();
//		}
	}	
	
	public static Button getCuentaButton() {
		return cuenta_button;
	}


	public static View getCuentaText() {
		// TODO Auto-generated method stub
		return cuenta_asociada;
	}
}
